package com.tuanzi.loan.web.bean.channel;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.repository.common.impl.SecondaryChannelGroupRepository;
import com.tuanzi.loan.business.service.common.CommProductChannelService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.response.channel.SecondaryChannelGroupResponse;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.CHANNEL_GROUP;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Component
@ViewScoped
public class ChannelInfoBean extends JSFInfoBean<CommProductChannel> {

    private static final long serialVersionUID = -2152778554108785342L;
    @Autowired
    private CommProductChannelService commProductChannelService;

    @Autowired
    private SecondaryChannelGroupRepository secondaryChannelGroupRepository;

    private List<SecondaryChannelGroupResponse> secondaryChannelGroups;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = commProductChannelService.findOne(NumberUtils.toLong(id));
            secondaryChannelGroups = secondaryChannelGroupRepository.findByPrimaryChannelGroup($.getChannelGroup());
        } else {
            $ = new CommProductChannel();
            secondaryChannelGroups = secondaryChannelGroupRepository.findAll();
            changeStatusToAdd();
        }
    }

    public void onSave() {
        Integer count = commProductChannelService.checkRepeatChannelCode($);
        if (count != null && count > 0) {
            addFiledMessage("渠道号重复");
            return;
        }
        if ($.getSecondaryChannelGroup() == null) {
            addFiledMessage("二级渠道分类不能为空");
            return;
        }
        $ = commProductChannelService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
    }

    public String onBatchAdd() {
        return redirect("/views/channel/channel/batch-info.xhtml");
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(CHANNEL_GROUP);
    }

    public void onChangeChannelGroup() {
        if ($.getChannelGroup() == null) {
            secondaryChannelGroups = secondaryChannelGroupRepository.findAll();
        } else {
            secondaryChannelGroups = secondaryChannelGroupRepository.findByPrimaryChannelGroup($.getChannelGroup());
        }
    }

    public List<SecondaryChannelGroupResponse> getSecondaryChannelGroups() {
        return secondaryChannelGroups;
    }
}
